﻿using BaseData.Domain;
using BaseData.ErrorCode;
using BaseData.Infrastructure.DTO;
using BaseData.Infrastructure.Permission;
using BaseData.Infrastructure.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.ReadAPI.Controllers
{
    /// <summary>
    /// 角色管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "Roles")]
    public class RolesController : ControllerBase
    {
        private readonly IUserService userService;
        private readonly IPermissionsService permService;

        public RolesController(IUserService userService,IPermissionsService permService)
        {
            this.userService = userService;
            this.permService = permService;
        }


        #region 角色管理
        /// <summary>
        ///  获取角色列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <param name="rolename">角色名称</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<PaggingResult<List<Roles>>>> GetRoleList(int index, int size, string? rolename)
        {
            var res = await userService.GetRoleList(index, size, rolename);
            return new APIResult<PaggingResult<List<Roles>>>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roles">实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddRole(Roles roles)
        {
            var res = await userService.AddRole(roles);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdRole(Roles roles)
        {
            var res = await userService.UpdRole(roles);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 修改角色状态
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdateRoleState([FromQuery]RoleStateDto dto)
        {
            var res = await userService.UpdateRoleState(dto);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<int>> DelRole(int id)
        {
            var res = await userService.DelRole(id);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 导出角色信息
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> ExportRole()
        { 
            var roles = await userService.GetRoles();
            return File(ExcelExporter.ExportToExcel(roles), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "角色信息.xlsx");//用于返回文件响应
            //三个参数:第一个参数是文件内容,第二个参数是文件的 MIME 类型,第三个参数是文件的名称
        }
        #endregion

        #region 权限管理
        /// <summary>
        /// 显示权限树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<PermissionTree>>> GetPermissionTrees()
        {
            var res = await permService.GetPermissionTrees(0);
            return new APIResult<List<PermissionTree>>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<Roles>> GetRoles(int id)
        {
            var res = await userService.GetRoles(id);
            return new APIResult<Roles>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddRolePermission(AddRolePermissionDto dto)
        {
            var res = await permService.AddRolePermission(dto);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        /// <summary>
        /// 根据角色获取权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<int>>> GetPermissionByRole(int roleId)
        {
            var res = await permService.GetPermissionByRole(roleId);
            return new APIResult<List<int>>
            {
                Code = CodeEnum.Success,
                Message = "",
                Data = res
            };
        }
        #endregion
    }
}
